﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Mozlite.Components;
using Mozlite.Components.Modules;
using pgs = Mozlite.Components.Modules.PageGroups;

namespace Mozlite.WebUI.Mvc4.Backend.Controllers
{
    /// <summary>
    /// 内容控制器。
    /// </summary>
    [AuthorizeEx(RoleLevel.Administrator)]
    public class ContentController : BaseController
    {
        #region page groups
        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <returns>返回分组试图。</returns>
        public ActionResult PageGroups(string cultureName = null)
        {
            ViewBag.CultureName = cultureName ?? CultureName;
            return View(new PageModule());
        }
        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <returns>返回分组试图。</returns>
        [HttpPost]
        public ActionResult QueryPageGroups(string cultureName = null)
        {
            var groups = pgs.LoadCategories(ModuleHelper.Page, cultureName);
            return Json(new
            {
                data = groups.ConvertAll(group => new
                {
                    GroupID = group.CategoryID,
                    GroupName = group.CategoryName,
                    RoleLevel = Local(group.RoleLevel),
                    Key = group.CategoryKey,
                    CreateDate = group.CreateDate.ToString(),
                    Description = group.Description
                }),
                size = groups.Count
            });
        }

        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <returns>页面分组。</returns>
        public ActionResult EditPageGroup(int? groupid = null, string cultureName = null)
        {
            var group = groupid == null ? new PageGroup() : pgs.GetCategory(groupid.Value, cultureName ?? CultureName);
            return View(group);
        }

        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <param name="ids">分组ID。</param>
        /// <returns>页面分组。</returns>
        [HttpDelete]
        public ActionResult DeletePageGroup(string ids, string cultureName = null)
        {
            if (ids == null || !pgs.Delete<PageModule>(ids, cultureName ?? CultureName))
                return JsonLocalError("PageGroup_DeletedError");
            return JsonLocalSuccess("PageGroup_Deleted");
        }

        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <returns>页面分组。</returns>
        [HttpPost]
        public ActionResult EditPageGroup(PageGroup model)
        {
            if (model.Save())
                ShowLocalSuccess("PageGroup_SaveSuccess");
            else
                ShowLocalError("PageGroup_SaveError");
            return View(model);
        }
        /// <summary>
        /// 判断分组名称。
        /// </summary>
        /// <param name="categoryName">分组名称。</param>
        /// <param name="categoryID">分组ID。</param>
        /// <param name="cultureName">区域语言。</param>
        /// <returns>返回试图结果。</returns>
        public ActionResult CheckGroupName(string categoryName, int categoryID, string cultureName) {
            var group = new PageGroup{ CategoryName = categoryName, CategoryID = categoryID, CultureName = cultureName, ParentID = 0};
            if (group.IsNameExisted())
                return JsonLocalCheckResult("PageGroup_DuplicateCategoryName");
            return JsonCheckResult();
        }
        /// <summary>
        /// 判断分组唯一键。
        /// </summary>
        /// <param name="categoryKey">分组唯一键。</param>
        /// <param name="categoryID">分组ID。</param>
        /// <param name="cultureName">区域语言。</param>
        /// <returns>返回试图结果。</returns>
        public ActionResult CheckGroupKey(string categoryKey, int categoryID, string cultureName)
        {
            var group = new PageGroup { CategoryKey = categoryKey, CategoryID = categoryID, CultureName = cultureName };
            if (group.IsKeyExisted())
                return JsonLocalCheckResult("Key_Duplicate");
            return JsonCheckResult();
        }
        /// <summary>
        /// 排序。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDown"></param>
        /// <param name="cultureName"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SortPageGroup(int id, bool isDown, string cultureName)
        {
            var group = pgs.GetCategory(id, cultureName);
            if (group == null || !pgs.Sorted(group, isDown))
                return JsonLocalError("Action_Error");
            return JsonLocalSuccess("Action_Sorted");
        }
        #endregion

        #region pages
        /// <summary>
        /// 页面分组。
        /// </summary>
        /// <returns>返回分组试图。</returns>
        public ActionResult Pages()
        {
            return View();
        }
        #endregion

    }
}
